<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" version="-//W3C//DTD XHTML 1.1//EN" dir="ltr">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script language="javascript" type="text/javascript" src="js/jquery.min.js"></script>
    <script language="javascript" type="text/javascript" src="js/json2.js"></script>
    <script language="javascript" type="text/javascript" src="js/jsonssproxy.js"></script>
    <link href="css/samples.css" rel="stylesheet" type="text/css">
    <title>Multi-service demo</title>
</head>
<body><div id="page-content">
 
    <h1>Multi-service demo</h1>

    <p class="bar">
        <input id="doCreate" type="button" value="Create" onclick="doCreate();">
        <input id="doBehold" type="button" value="Get Creature" onclick="doBehold();">
        <input id="doBehave" type="button" value="Try to put Creature" onclick="doBehave();">
    </p>
    <p>
        Server (<a href="multiservice.php?service-source">view source</a>) exposes three services:
        <ul>
            <li>VerbService - with one method Create()</li>
            <li>NounService - with one read-only read-once property Creature</li>
            <li>QuestionService - with one Created</li>
        </ul>
    </p>
    <p>
        Exchage log:
    </p>
    <pre id="log" class="console"></pre>

    <div class="cr_footer">Released under GPLv2, (C) 2012 Serge V. Izmaylov</div>

    <script type="text/javascript">
        //<![CDATA[
        var prx;

        function doCreate() {
            try {
                $("#log").append("Calling VerbService.Create()...\r\n");
                var res = prx.VerbService.Create();
            } catch (x) {
                if (x instanceof JSONSSError) {
                    $("#log").append("VerbService.Create() fault: " + x.message + "\r\nadditional data: " + JSON.stringify(x.data) + "\r\n");
                    alert("callSayHello fault: " + x.message + "\r\n" + JSON.stringify(x.data));
                } else {
                    $("#log").append("Exception while calling VerbService.Create(): " + x.toString() + "\r\n");
                    alert("callSayHello exception: " + x.toString());
                }
            }
        }

        function doBehold() {
            try {
                $("#log").append("Reading NounService.Creature...\r\n");
                var res = prx.NounService.getCreature();
                $("#log").append("New creature is " + res + "\r\n");
            } catch (x) {
                if (x instanceof JSONSSError) {
                    $("#log").append("NounService.getCreature() fault: " + x.message + "\r\nadditional data: " + JSON.stringify(x.data) + "\r\n");
                    alert("callSayHello fault: " + x.message + "\r\n" + JSON.stringify(x.data));
                } else {
                    $("#log").append("Exception while calling NounService.getCreature(): " + x.toString() + "\r\n");
                    alert("callSayHello exception: " + x.toString());
                }
            }
        }

        function doBehave() {
            try {
                $("#log").append("Writing NounService.Creature...\r\n");
                var res = prx.NounService.setCreature("MyCreature");
                $("#log").append("Hmmm... succeed. O_o\r\n");
            } catch (x) {
                if (x instanceof JSONSSError) {
                    $("#log").append("NounService.setCreature() fault: " + x.message + "\r\nadditional data: " + JSON.stringify(x.data) + "\r\n");
                    alert("callSayHello fault: " + x.message + "\r\n" + JSON.stringify(x.data));
                } else {
                    $("#log").append("Exception while calling NounService.setCreature(): " + x.toString() + "\r\n");
                    alert("callSayHello exception: " + x.toString());
                }
            }
        }

        function onCreated(c) {
            $("#log").append("QuestionService reports: Have new creature for you - " + c.toString() + "\r\n");
        }

        function checkEvents() {
            try {
                prx.QuestionService.ping();
            } catch (x) {
                if (x instanceof JSONSSError) {
                    $("#log").append("QuestionService.ping() fault: " + x.message + "\r\nadditional data: " + JSON.stringify(x.data) + "\r\n");
                    alert("callSayHello fault: " + x.message + "\r\n" + JSON.stringify(x.data));
                } else {
                    $("#log").append("Exception while calling QuestionService.ping(): " + x.toString() + "\r\n");
                    alert("callSayHello exception: " + x.toString());
                }
            }
            setTimeout(checkEvents, 3000);
        }

        $(document).ready(function () {
            try {
                var serviceurl = location.href;
                serviceurl = serviceurl.replace(/.htm$/, ".php");
                $("#log").append("Loading service interface:\r\n");
                prx = JSONSSProxy.fromUrl(serviceurl);
                $("#log").append("Got proxy object" + "\r\n");
                prx.QuestionService.onCreated = onCreated;
                checkEvents();
            } catch (x) {
                if (x instanceof JSONSSError) {
                    $("#log").append("JSONSSProxy.fromUrl fault: " + x.message + "\r\n" + JSON.stringify(x.data) + "\r\n");
                    alert("JSONSSProxy.fromUrl fault: " + x.message + "\r\n" + JSON.stringify(x.data));
                } else {
                    $("#log").append("JSONSSProxy.fromUrl exception: " + x.toString() + "\r\n");
                    alert("JSONSSProxy.fromUrl exception: " + x.toString());
                }
            }
        });
        //]]>
    </script>
</div></body>
</html>
